package greentycoon.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import greentycoon.main.Main;
import greentycoon.main.VariablesGlobales;
import greentycoon.ville.Ville;
import greentycoon.ville.amenagement.TransportPublique.StationDeBus;
import greentycoon.ville.amenagement.TransportPublique.StationDeMetro;
import greentycoon.ville.amenagement.TransportPublique.StationLocationVelo;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class FenetreGestionTransport extends JFrame{
	
	/*Attributs*/
	private boolean busUnlock = false;
	private boolean metroUnlock = false;
	private boolean veloUnlock = false;
	private int tauxInvestissementBus = 0;
	private int tauxInvestissementMetro = 0;
	private int tauxInvestissementVelo = 0;

	public FenetreGestionTransport(){
		setTitle("Gestion des transports");
		setSize(550, 300);
	    setLocation(300, 300);
	    setVisible(false);
	    setLayout(null);
	    setAlwaysOnTop(true);
	    construire();

	}

	private void construire() {
		
		getContentPane().removeAll();
		if(busUnlock)
		{
			/*Bus*/
			JLabel busLabel = new JLabel("Bus");
			busLabel.setBounds(30, 20, 50, 50);
			add(busLabel);
		
			final JLabel tauxInvestissementBusLabel = new JLabel(tauxInvestissementBus+"%");
			tauxInvestissementBusLabel.setBounds(260, 20, 50, 50);
			add(tauxInvestissementBusLabel);
		
			final JLabel valeurInvestissementBusLabel = new JLabel(Ville.calculInvestissementTransportBus()+" greenFlouz");
			valeurInvestissementBusLabel.setBounds(295, 20, 200, 50);
			add(valeurInvestissementBusLabel);
		
			JSlider busSlider = new JSlider(JSlider.HORIZONTAL,0,100,tauxInvestissementBus);
			busSlider.setBounds(60, 20, 200, 50);
			busSlider.setMajorTickSpacing(25);
			busSlider.setMinorTickSpacing(5);
			busSlider.addChangeListener(new ChangeListener() {
			
				@Override
				public void stateChanged(ChangeEvent e) {
					JSlider source = (JSlider)e.getSource();
					if (!source.getValueIsAdjusting()) {
						Ville.setTauxInvestissementTransportBus((int)source.getValue());
					 	tauxInvestissementBusLabel.setText(Integer.toString((int)source.getValue())+"%");
					 	valeurInvestissementBusLabel.setText(Ville.calculInvestissementTransportBus()+" greenFlouz");
					 	tauxInvestissementBus = (int)source.getValue();
					}
				}
			});
			add(busSlider);
		}else{
			JButton unLockBusButton = new JButton("Mettre en place un reseau de Bus : "+ StationDeBus.prix+" greenFlouz");
			unLockBusButton.setBounds(30, 20, 400, 30);
			
			
			unLockBusButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if (VariablesGlobales.argent - StationDeBus.prix >= 0)
					{
						VariablesGlobales.argent -= StationDeBus.prix;
						MenuCreation.ville.rafraichirInformations();
						
						busUnlock = true;
						construire();
					}
					
				}
			});
			add(unLockBusButton);
			}
		
		/*Metro*/
		if(metroUnlock)
		{
			JLabel metroLabel = new JLabel("Metro");
			metroLabel.setBounds(20, 100, 100, 50);
			add(metroLabel);
		
			final JLabel tauxInvestissementMetroLabel = new JLabel(tauxInvestissementMetro+"%");
			tauxInvestissementMetroLabel.setBounds(260, 100, 50, 50);
			add(tauxInvestissementMetroLabel);
		
			final JLabel valeurInvestissementMetroLabel = new JLabel(Ville.calculInvestissementTransportMetro()+" greenFlouz");
			valeurInvestissementMetroLabel.setBounds(295, 100, 200, 50);
			add(valeurInvestissementMetroLabel);
		
			JSlider metroSlider = new JSlider(JSlider.HORIZONTAL,0,100,tauxInvestissementVelo);
			metroSlider.setBounds(60, 100, 200, 50);
			metroSlider.setMajorTickSpacing(25);
			metroSlider.setMinorTickSpacing(5);
			metroSlider.addChangeListener(new ChangeListener() {
			
				@Override
				public void stateChanged(ChangeEvent e) {
					JSlider source = (JSlider)e.getSource();
					if (!source.getValueIsAdjusting()) {
						Ville.setTauxInvestissementTransportMetro((int)source.getValue());
						tauxInvestissementMetroLabel.setText(Integer.toString((int)source.getValue())+"%");
						valeurInvestissementMetroLabel.setText(Ville.calculInvestissementTransportMetro()+" greenFlouz");
						tauxInvestissementMetro = (int)source.getValue();
					}
				}
			});
			add(metroSlider);
		}else{
			JButton unLockMetroButton = new JButton("Mettre en place un reseau de Metro : "+ StationDeMetro.prix+" greenFlouz");
			unLockMetroButton.setBounds(30, 100, 400, 30);
			
			
			unLockMetroButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if (VariablesGlobales.argent - StationDeMetro.prix >= 0)
					{
						VariablesGlobales.argent -= StationDeMetro.prix;
						MenuCreation.ville.rafraichirInformations();
						
						metroUnlock = true;
						construire();
					}
					
				}
			});
			add(unLockMetroButton);
		}
		
		/*velo*/
		if(veloUnlock)
		{
			JLabel veloLabel = new JLabel("Velo");
			veloLabel.setBounds(20, 180, 60, 50);
			add(veloLabel);
			
			final JLabel tauxInvestissementVeloLabel = new JLabel(tauxInvestissementVelo+"%");
			tauxInvestissementVeloLabel.setBounds(260, 180, 50, 50);
			add(tauxInvestissementVeloLabel);
		
			final JLabel valeurInvestissementVeloLabel = new JLabel(Ville.calculInvestissementTransportVelo()+" greenFlouz");
			valeurInvestissementVeloLabel.setBounds(295, 180, 200, 50);
			add(valeurInvestissementVeloLabel);
		
			JSlider veloSlider = new JSlider(JSlider.HORIZONTAL,0,180,tauxInvestissementVelo);
			veloSlider.setBounds(60, 180, 200, 50);
			veloSlider.setMajorTickSpacing(25);
			veloSlider.setMinorTickSpacing(5);
			veloSlider.addChangeListener(new ChangeListener() {
			
				@Override
				public void stateChanged(ChangeEvent e) {
					JSlider source = (JSlider)e.getSource();
					if (!source.getValueIsAdjusting()) {
						Ville.setTauxInvestissementTransportVelo((int)source.getValue());
						tauxInvestissementVeloLabel.setText(Integer.toString((int)source.getValue())+"%");
						valeurInvestissementVeloLabel.setText(Ville.calculInvestissementTransportVelo()+" greenFlouz");
						tauxInvestissementVelo = (int)source.getValue();
					}
				}
			});
			add(veloSlider);
		}else{
			JButton unLockVeloButton = new JButton("Mettre en place des bornes pour Velo : "+ StationLocationVelo.prix+" greenFlouz");
			unLockVeloButton.setBounds(30, 180, 400, 30);
			
			
			unLockVeloButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if (VariablesGlobales.argent - StationLocationVelo.prix >= 0)
					{
						VariablesGlobales.argent -= StationLocationVelo.prix;
						MenuCreation.ville.rafraichirInformations();
						
						veloUnlock = true;
						construire();
					}
					
				}
			});
			add(unLockVeloButton);
		}

		repaint();
	}
	

	
}
